
weight <- r2_choices_num %>%
  filter(discuss_type %in% c("control", "discussion_full")) %>%
  group_by(discuss_type) %>% count_prop(discuss_type, phase) %>%
  group_by(discuss_type) %>%
  summarise(
    ratio_phase_21 = sum(phase == "phase_2") / sum(phase == "phase_1"),
  ) %>%
  summarise(rel_weight = ratio_phase_21[1] / ratio_phase_21[2]) %>%
  pull(rel_weight)


weight %>% write_stat("outputs/stats/sampling_weight.tex", digits = 2)

r2_choices_num_weights <- r2_choices_num %>%
  filter(discuss_type %in% c("control", "discussion_full")) %>%
  mutate(
    weight = case_when(
      discuss_type == "discussion_full" & phase == "phase_2" ~ weight,
      TRUE ~ 1
    )
  )


model_list_sampling_weights <- list(
  "Chose worker in private\noutcome round (=1)" = feols_custom(
    r2_choose_comparator ~ pair_includes_trans * (discussion_full),
    data = r2_choices_num_weights %>% filter(discuss_type %in% c("control", "discussion_full")),
    fixef = NULL,
    weights = weight,
    cluster = "group_id",
    ri = FALSE,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    coef_keep = "^pair_includes_trans$|(^pair_includes_trans\\:discussion_full$)|^discussion_full$|item_diff|r2_reliability_diff|r2_reliability_shown",
    coef_omit = "group_control|stratum_id|delivery_incentive_exp"
  ),
  "Chose worker in private\noutcome round (=1)" = feols_custom(
    r2_choose_comparator ~ pair_includes_trans_alt + pair_includes_trans_alt*(video_type_placebo + video_type_treatment + discussion_full + factor(stratum_id) + delivery_incentive_exp + phase) + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_choices_num_weights %>% filter(discuss_type %in% c("control", "discussion_full")) %>% count_prop(phase),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order_in_pair", "phase"),
    cluster = "group_id",
    ri = FALSE,
    n_sims = ri_sims,
    weights = weight,
    stratum = "stratum_id",
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = "discussion_full",
      interact = "pair_includes_trans",
      group_control = TRUE
    ),
    var_types = var_types_spec,
    coef_keep = "^pair_includes_trans$|(^pair_includes_trans\\:discussion_full$)|^discussion_full$|item_diff|r2_reliability_diff|r2_reliability_shown",
    coef_omit = "group_control|stratum_id|delivery_incentive_exp"
  ),
  "Chose trans in private\noutcome round (=1)\n(pairs with trans only)" = feols_custom(
    r2_choose_trans ~ video_type_placebo + video_type_treatment + discussion_full + factor(stratum_id) + delivery_incentive_exp + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_choices_num_weights %>% filter(discuss_type %in% c("control", "discussion_full")) %>% filter(!is.na(r2_choose_trans)),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order_in_pair", "phase"),
    cluster = "group_id",
    weights = weight,
    ri = FALSE,
    n_sims = ri_sims,
    stratum = "stratum_id",
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = "discussion_full",
      interact = "pair_includes_trans",
      group_control = TRUE
    ),
    var_types = var_types_spec,
    coef_keep = "^pair_includes_trans$|(^pair_includes_trans\\:discussion_full$)|^discussion_full$|item_diff|r2_reliability_diff|r2_reliability_shown",
    coef_omit = "group_control|stratum_id|delivery_incentive_exp"
  )
)


tex_export(
  model_list_sampling_weights,
  file = "outputs/tables/main_table_sampling_weights.tex",
  coef_rename = coef_label,
  gof_map = fe_label_no_fe,
  coef_reorder = c("pair_includes_trans:discussion_full"),
  coef_omit = vars_to_regex(c("stratum_id", "Intercept", "video", "group_control", "pair_includes_trans_alt$", "phase", control_vars, "r2_reliability|item", "delivery")),
  dep_means = list(
    "Mean: No discussion (private) + worker is non-trans" = "discuss_type == 'control' & pair_includes_trans == 0",
    "Mean: No discussion (private) + worker is T" = "discuss_type == 'control' & pair_includes_trans == 1"
  ),
)
